package com.feng.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.feng.entity.Admin;
import com.feng.util.db.DBHelp;
import com.feng.util.db.RowMapper;

public class AdminDao {

	DBHelp<Admin> db = new DBHelp<Admin>();
	
	
	private class AdminRowMapper implements RowMapper<Admin>{

		public Admin mapRow(ResultSet rs) throws SQLException {
			Admin a = new Admin();
			a.setId(rs.getString("id"));
			a.setUsername(rs.getString("username"));
			a.setPassword(rs.getString("password"));
			a.setEmail(rs.getString("email"));
			a.setIp(rs.getString("ip"));
			a.setRole(rs.getString("role"));
			a.setLastAccessTime(rs.getString("lastaccesstime"));
			a.setCreateTime(rs.getString("createtime"));
			return a;
		}
		
		
	}
	public Admin findByName(Admin a) {
		String sql = "select id,username,`password`,email,ip,role,lastaccesstime,createtime from t_admin where username=? order by createtime desc";
		return db.executeQueryToObject(sql, new AdminRowMapper(), a.getUsername());
	}
	public void update(Admin a) {
		String sql = "update t_admin set ip=? , lastaccesstime=? where id=?";
		db.executeSQL(sql, a.getIp(),a.getLastAccessTime(),a.getId());
	}
}
